import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { Register } from './register.dto'
import { CaptchaChallenge } from '../captcha'

export function getUser (id: number | string, params: CreateQueryParams) {
  return service({
    url: `/users/${id}`,
    method: 'get',
    params,
    paramsSerializer: crudParamsSerializer
  })
}

export function getUserAuthed () {
  return service({
    url: '/auth/me',
    method: 'get'
  })
}

// export function login (data: {username: string; password: string}, captcha: CaptchaChallenge) {
//   return service.post(
//     '/auth/login',
//     data,
//     {
//       headers: {
//         captcha: captcha.toHeaderString()
//       }
//     }
//   )
// }

export function login (data: {username: string; password: string}) {
  return service.post(
    '/auth/login',
    data
  )
}

export function logout () {
  return service.post<boolean>(
    '/auth/logout'
  )
}

export function register (data: Partial<Register>, captcha: CaptchaChallenge) {
  return service.post('/auth/register',
    data,
    {
      headers: {
        captcha: captcha.toHeaderString()
      }
    }
  )
}
